import axios from "axios"
import LocalStorageUtil from './LocalStorageUtil'

axios.defaults.timeout = 5000; //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/json'

function get(url, header,param, response) {
    let config = {
        headers: header,
        params: param
    };
    axios.get(url, config)
        .then(function (data) {
            response.onSuccess(data);
        })
        .catch(function (err) {
            response.onFail(err);
        });
}

function post(url, header, parms, response) {
    let config = {
        headers: header
    };
    axios.post(url, parms, config)
        .then(function (data) {
            response.onSuccess(data.data);
        })
        .catch(function (err) {
            response.onFail(err);
        });
}

function put(url, header, parms, response) {
    let config = {
        headers: header
    };
    axios.put(url, parms, config)
        .then(function (data) {
            response.onSuccess(data.data);
        })
        .catch(function (err) {
            response.onFail(err);
        });
}

function Delete(url, header, response) {
    let config = {
        headers: header
    };
    axios.delete(url, config)
        .then(function (data) {
            response.onSuccess(data.data);
        })
        .catch(function (err) {
            response.onFail(err);
        });
}

function getToken(){
    let token = LocalStorageUtil.getItem('token')
    return token
}

let header = {};

let ACApi = {};

// 数据相关
// let BaseUrl = "http://192.168.1.123:9000/v2/support"
let BaseUrl = "http://47.97.26.165:9000/v2/support"

// 获取登录token
ACApi.getTokenApi = function(sign, response) {
    let url = BaseUrl + '/auth/login'
    let params = {
        id: 'xiaoqiang',
        sign: sign,
        category: 0
    }
    post(url,header,params, response)
}

// 获取省份信息
ACApi.getareaFirstlevel = function(response) {
    header.Authorization = getToken()
    let url = BaseUrl + '/area/getfirstlevel'
    get(url,header,{}, response)
}

// 获取市级信息
ACApi.getareaAreas = function(pares, response) {
    header.Authorization = getToken()
    let url = BaseUrl + '/area/getareas'
    get(url,header,pares, response)
}

// 上报呼救信息
ACApi.patientsCalls = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/calls'
    post(url,header,params, response)
}

// 获取患者基本信息数据
ACApi.getpatientsUsers = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/users/' + params.id
    get(url,header,{}, response)
}

// 编辑患者基本信息数据
ACApi.updataPatientsUsers = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/users'
    put(url,header,params, response)
}

// 保存患者基本信息数据
ACApi.patientsUsers = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/users'
    post(url,header,params, response)
}

// 获取患者健康信息数据
ACApi.getpatientsHealths = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/healths/' + params.id
    get(url,header,{}, response)
}

// 编辑患者健康信息数据
ACApi.updataPatientsHealths = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/healths'
    put(url,header,params, response)
}

// 保存患者健康信息数据
ACApi.patientsHealths = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/healths'
    post(url,header,params, response)
}

// 获取患者亲友信息数据
ACApi.getpatientsRelatives = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/'+ params.id +'/relatives'
    get(url,header,{}, response)
}

// 新增亲友信息数据
ACApi.patientsRelatives = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/relatives'
    post(url,header,params, response)
}

// 更新亲友信息数据
ACApi.updataPatientsRelatives = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/relatives'
    put(url,header,params, response)
}

// 删除亲友信息数据
ACApi.deletePatientsRelatives = function(id,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/relatives/' + id
    Delete(url,header, response)
}

// 服务记录
ACApi.serviceRecord = function(params,response) {
    header.Authorization = getToken();
    let url = BaseUrl + '/patients/calls/pages'
    get(url,header,params, response)
}

export default ACApi
